<template>
<el-select v-model="selectVal" :filterable="filterable" :multiple="multiple" :placeholder="placeholder" clearable @change="changeEvt">
    <el-option v-for="item in options" :key="item.value" :label="item.title" :value="item.id+''"></el-option>
</el-select>
</template>

<script>
import {
    getPickerApi
} from "@/api/system/dictionary/dictionary.js";

export default {
    name: "PickerView",
    data() {
        return {
            selectVal: "",
            options: [],
        };
    },
    props: {
        value: {
            type: [String, Number, Array],
            default: function () {
                return "";
            },
        },
        type: {
            type: String,
            default: "",
        },
        filterable: {
            type: Boolean,
            default: function () {
                return true;
            },
        },
        multiple: {
            type: Boolean,
            default: function () {
                return false;
            }
        },
        placeholder: {
            type: String,
            default: "请选择",
        },
        filter: {
            type: Object,
            default: function () {
                return {};
            },
        },
    },
    watch: {
        value: {
            immediate: true,
            handler(val) {
                if (this.multiple) {
                    this.selectVal = val;
                    return;
                } else {
                    this.selectVal = val + '';
                }
            },
        },
        type: {
            immediate: true,
            handler(value) {
                let _this = this;
                let options = {
                    type: value,
                };
                options.filter = this.filter;

                getPickerApi(options).then((res) => {
                    _this.options = res.data.list;
                });
            },
        },
    },
    methods: {
        changeEvt() {
            this.$emit("input", this.selectVal);
            let selectRow = this.options.find(e => e.id == this.selectVal);
            this.$emit("change", selectRow);
        },
    },
};
</script>
